﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.DataAccess;

namespace YiYiTong.Rocket.Business
{
    public class RoleBiz
    {
        private static readonly ModelHandler<RoleInfo> modelHelper = new ModelHandler<RoleInfo>();

        #region Instance
        private RoleBiz()
        {
        }
        private static RoleBiz _instance;

        private static object _syncObject = new object();

        public static RoleBiz Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new RoleBiz();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion

        /// <summary>
        /// 根据角色编号获取角色信息
        /// </summary>
        /// <param name="roleId">角色编号</param>
        /// <returns></returns>
        public RoleInfo GetRoleInfoById(int roleId)
        {
            RoleInfo role = new RoleInfo();
            DataTable dtRole = RoleDataAccess.Instance.GetRoleInfo(roleId);
            if (dtRole != null && dtRole.Rows.Count != 0)
            {
                role = modelHelper.FillModel(dtRole.Rows[0]);
            }
            return role;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="roleCode">角色代码</param>
        /// <param name="roleName">角色名称</param>
        /// <param name="remark">备注</param>
        /// <param name="strOutPut"></param>
        /// <returns></returns>
        public int AddRole(string roleCode, string roleName, string remark, out string strOutPut)
        {
            int ret = 0;
            strOutPut = "添加角色失败";
            try
            {
                DataTable dtRole = RoleDataAccess.Instance.GetRoleInfo(roleName);
                if (dtRole != null && dtRole.Rows.Count != 0)
                {
                    ret = -1;
                    strOutPut = "添加角色失败,角色名称已存在";
                    return ret;
                }

                string createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ret = RoleDataAccess.Instance.AddRole(roleCode, roleName, createTime, 0, remark);
                if (ret > 0)
                {
                    strOutPut = "添加角色成功";
                }
            }
            catch (Exception)
            {
                ret = -99;
                strOutPut = "出现异常，请联系技术人员";
            }
            return ret;
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="roleId">角色编号</param>
        /// <param name="roleCode">角色代码</param>
        /// <param name="roleName">角色名称</param>
        /// <param name="remark">备注</param>
        /// <param name="strOutPut"></param>
        /// <returns></returns>
        public int EditRole(int roleId, string roleCode, string roleName, string remark, out string strOutPut)
        {
            int ret = 0;
            strOutPut = "修改角色失败";
            DataTable dtRole = RoleDataAccess.Instance.GetRoleInfo(roleId);
            if (dtRole == null || dtRole.Rows.Count == 0)
            {
                ret = -1;
                strOutPut = "修改角色失败,角色不存在";
                return ret;
            }
            try
            {
                dtRole = RoleDataAccess.Instance.GetRoleInfo(roleName);
                if (dtRole != null && dtRole.Rows.Count != 0)
                {
                    ret = RoleDataAccess.Instance.EditRole(roleId, roleCode, remark);
                }
                else
                {
                    ret = RoleDataAccess.Instance.EditRole(roleId, roleCode, roleName, remark);
                }
                if (ret > 0)
                {
                    strOutPut = "修改角色成功";
                }
            }
            catch (Exception)
            {
                ret = -99;
                strOutPut = "出现异常，请联系技术人员";
            }
            return ret;
        }

        /// <summary>
        /// 删除角色(逻辑删除)
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public int DelRole(int roleId, out string strOutPut)
        {
            int ret = 0;
            strOutPut = "删除角色失败";
            try
            {
                DataTable dtMenu = RoleDataAccess.Instance.GetRoleInfo(roleId);
                if (dtMenu == null && dtMenu.Rows.Count == 0)
                {
                    ret = -1;
                    strOutPut = "删除角色失败,角色不存在";
                    return ret;
                }
                else
                {
                    DataTable dt = RoleDataAccess.Instance.GetUserRole(roleId);
                    if (dt != null && dt.Rows.Count != 0)
                    {
                        ret = -1;
                        strOutPut = "删除角色失败,用户已设置角色";
                        return ret;
                    }

                    ret = RoleDataAccess.Instance.DelRole(roleId);
                }

                if (ret > 0)
                {
                    strOutPut = "删除角色成功";
                }
            }
            catch
            {
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 查询角色
        /// </summary>
        /// <param name="roleCode">角色代码</param>
        /// <param name="roleName">角色名称</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageRows">每页多少行</param>
        /// <returns>返回角色列表，为空返回null</returns>
        public DataSet GetPageList(string roleCode, string roleName, int pageIndex, int pageRows)
        {
            return RoleDataAccess.Instance.GetPageList(roleCode, roleName, pageIndex, pageRows);
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllRoleList()
        {
            return RoleDataAccess.Instance.GetAllRoleList();
        }

        /// <summary>
        /// 根据角色编号获取角色菜单项信息
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public DataTable GetRoleMenuList(int roleID)
        {
            return RoleDataAccess.Instance.GetRoleMenuList(roleID);
        }

        /// <summary>
        /// 根据角色编号获取角色功能项信息
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public DataTable GetRoleActionList(int roleID)
        {
            return RoleDataAccess.Instance.GetRoleActionList(roleID);
        }

        /// <summary>
        /// 设置角色菜单项
        /// </summary>
        /// <param name="uid">角色编号</param>
        /// <param name="menuIds">菜单，多个以,逗号隔开</param>
        /// <returns>是否设置成功</returns>
        public int SetRoleMenu(int roleID, string menuIds)
        {
            int ret = 0;

            string[] menuIdList = Utils.GetStrings(menuIds, ",");
            if (menuIdList.Length == 0 || menuIdList == null)
                return ret;

            try
            {
                //删除原有用户角色关联
                RoleDataAccess.Instance.DeleteRoleMenu(roleID);

                //开始设置
                ret = RoleDataAccess.Instance.SetRoleMenu(roleID, menuIdList);

            }
            catch (Exception)
            {
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 设置角色功能项
        /// </summary>
        /// <param name="uid">角色编号</param>
        /// <param name="actionIds">功能id，多个以,逗号隔开</param>
        /// <returns>是否设置成功</returns>
        public int SetRoleAction(int roleID, string actionIds)
        {
            int ret = 0;

            string[] actionIdList = Utils.GetStrings(actionIds, ",");
            if (actionIdList.Length == 0 || actionIdList == null)
                return ret;

            try
            {
                //删除原有用户角色关联
                RoleDataAccess.Instance.DeleteRoleAction(roleID);

                //开始设置
                ret = RoleDataAccess.Instance.SetRoleAction(roleID, actionIdList);

            }
            catch (Exception)
            {
                ret = -99;
            }
            return ret;
        }
    }
}